<!--suppress ALL -->
<template>
	<el-dialog title=" 文件上传" :close-on-click-modal="false" :visible.sync="visible">
		<el-form :model="dataForm" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="130px">

			<el-form-item label="名单文件" prop="fileList">
				<el-upload class="upload-demo" action="as" :limit="1" accept=".xlsx,.xls" :http-request="taskUploadHttpRequest" :file-list="taskUploadFileList" :auto-upload="true" :show-file-fist="false" multiple>
					<i class="el-icon-upload" title="发送图片">文件上传</i>
					<div slot="tip" class="el-upload__tip">
						支持扩展名:xlsx的excel文件
						<el-button style="margin-left: 20px;" @click="downloadFile()" size="small" type="primary">模板下载</el-button>
					</div>
				</el-upload>
				
			</el-form-item>
		</el-form>
		<span slot="footer" class="dialog-footer"><el-button @click="dataFormSubmit()">保存</el-button>
    </span>
	</el-dialog>
</template>

<script>
	export default {
		data() {
			return {
				dataForm: {},
				visible: false,
				ignoreOne: false,
				taskUploadFileList: [],
				ftpPath: '',
				analysisStatus: '',
				file:null,
			}
		},
		created() {},
		methods: {
			init: function() {
				console.log("2222")
				this.taskUploadFileList = []
				var that = this
				this.visible = true
			},
			downloadFile: function() {
				var that = this
				this.$http.get('cc/customer/model/download', { responseType: 'blob' })  
				    .then(response => {  
				        const url = window.URL.createObjectURL(new Blob([response.data]));  
				        const link = document.createElement('a');  
				        link.href = url;  
				        link.setAttribute('download', '客户信息模板.xlsx');  
				        document.body.appendChild(link);  
				        link.click();  
				        // 清理  
				        document.body.removeChild(link);  
				        window.URL.revokeObjectURL(url);  
				    })  
				    .catch(error => console.error('Download error:', error));
			},
			dataFormSubmit() {
				this.$refs['dataForm'].validate((valid) => {
					var that = this
					console.log("that.file",that.file)
					if(that.file == null) {
						that.$message.error('请提交excel文件')
						return
					}
					const formData = new FormData();  
					formData.append('file', that.file);  
					this.$http.post('/cc/customer/model/upload',formData).then(res => {
						if(res.data.code == '0'){
							that.$message({
								message: '上传成功,后台正在解析中,请稍后刷新',
								type: 'success',
								duration: 1500,
								onClose: () => {
									that.visible = false;
									that.$emit('refreshDataList')
								}
							});
						}else{
							that.file = null
							that.$message.error(resp.data.msg)
						}
					})
				})
			},
			taskUploadHttpRequest(fe) {
				var file = fe.file
				var that = this
				let formData = new FormData()
				formData.append('file', file)
				console.log(fe)
				this.file = file
				// btwApiUtil.post('common.uploadfile.single', {}, function(resp) {
				// 	that.taskUploadFileList = []
				// 	//ftp地址
				// 	that.ftpPath = resp.data.ftpPath
				// }, formData)
			}
		}
	}
</script>